package com.hs.ding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.bai.pojo.Order;
import com.hs.ding.mapper.NeedMapper;
import com.hs.ding.pojo.Need;
import com.hs.ding.pojo.Vehicle;
import com.hs.ding.mapper.VehicleMapper;
import com.hs.ding.service.IVehicleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YingFeng
 * @since 2022-06-16
 */
@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> implements IVehicleService {

    @Autowired
    private NeedMapper needMapper;

    @Autowired
    private VehicleMapper vehicleMapper;


    @Override
    public ResultBean findAllVehicle(Integer currentPage, Integer pageSize, String inputOrder, String inputName, String inputDeparture) {

        //查询状态为0
        QueryWrapper<Vehicle> qw = new QueryWrapper<>();
        qw.eq("v_status",0);

        //根据需求单号查询指定对象
        if (StringUtils.hasText(inputOrder)) {
            qw.like("v_order_id",inputOrder);
        }

       //分页构造器
        IPage<Vehicle> page = new Page<>(currentPage,pageSize);

        IPage<Vehicle> p = vehicleMapper.selectPage(page, qw);

        // 分页的的记录
        List<Vehicle> records = p.getRecords();

        //查询 每一个 need
        for (Vehicle vehicle : records) {
            System.out.println(vehicle.getOrderId());
            Need need = needMapper.selectById(vehicle.getOrderId());
            vehicle.setNeed(need);
        }

        Long total = p.getTotal();
        System.out.println("records = " + records);
        System.out.println("total = " + total);

        PageBean pageBean = new PageBean();

        pageBean.setList(records);
        pageBean.setTotal(total.intValue());

        ResultBean ok = new ResultBean();
        ok.setData(pageBean);
        return ok;
    }

    @Override
    public List<Vehicle> searchByCondition(Integer currentPage,
                                           Integer pageSize,
                                           String inputOrder,
                                           String inputName,
                                           String inputDeparture) {

        List<Vehicle> vehicles = vehicleMapper.selectByCondition(currentPage,pageSize,inputOrder,inputName,inputDeparture);

        System.out.println("vehicles = " + vehicles);

        return vehicles;
    }
}
